<?php
/**
 * Created by PhpStorm.
 * User: surface
 * Date: 2018/12/12
 * Time: 20:00
 */
namespace app\common\model;

class Room extends Common
{

    public function tableData($post,$isPage=true)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);
        $query = $this::with('defaultImage')
            ->field($tableWhere['field'])->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);

        if($isPage){
            $list = $query->paginate($limit);
            $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = $list->total();
        }else{
            $list = $query->select();
            $data = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
        }
        $re['code'] = 0;
        $re['msg'] = '';
        $re['data'] = $data;
        return $re;
    }

    public function updateRoom($room_id,$data=[])
    {
        return $this->save($data,['id'=>$room_id]);
    }

    public function getOne($room_id,$fields='*')
    {
        $result = [
            'status' => false,
            'data'   => [ ],
            'msg'    => '房间不存在'
        ];
        $data   = $this->where([ 'id' => $room_id ])->field($fields)->find();
        if($data) {
            $tmp_image = [];
            //单图
            $tmp_image[]   = [
                'goods_id'   => $data['id'],
                'image_id'   => $data['image_id'],
                'image_path' => _sImage($data['image_id']),
            ];
            $image_list = json_decode($data['image_list'], true);
            if(is_array($image_list)){
                foreach ($image_list as $img_key) {
                    $tmp_image[]   = [
                        'goods_id'   => $data['id'],
                        'image_id'   => $img_key,
                        'image_path' => _sImage($img_key),
                    ];
                }
            }
            $images['data'] = $tmp_image;
            $data['images']   = $images['data'];
            $result['data']   = $data;
            $result['msg']    = '查询成功';
            $result['status'] = true;
        }
        return $result;
    }

    /**
     * 默认排序
     * @param $post
     * @return mixed
     * User: wjima
     * Email:1457529125@qq.com
     * Date: 2018-01-11 16:32
     */
    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if (isset($post['name']) && $post['name'] != "") {
            $where[] = ['name', 'like', '%' . $post['name'] . '%'];
        }
        if (isset($post['id']) && $post['id'] != "") {
            $where[] = ['id', 'in', $post['id']];
        }

        if(isset($post['bn'])&& $post['bn'] != ""){
            $where[] = ['bn', 'like', '%'.$post['bn'].'%'];
        }

        $result['where'] = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $result['order'] = ['id' => 'desc'];
        return $result;
    }

    public function doAdd($data = [])
    {
        $result=$this->insert($data);
        if($result)
        {
            return $this->getLastInsID();
        }
        return $result;
    }

    public function defaultImage()
    {
        return $this->hasOne('Images','id','image_id')->field('id,url')->bind([ 'image_url' => 'url' ]);
    }
}